package cn.wolfcode.crm.service.imp;

import cn.wolfcode.crm.domain.AccountBook;
import cn.wolfcode.crm.domain.MemberInfo;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.mapper.AccountBookMapper;
import cn.wolfcode.crm.mapper.MemberInfoMapper;
import cn.wolfcode.crm.qo.MemberInfoQueryObject;
import cn.wolfcode.crm.qo.PageResult;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IMemberInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MemberInfoServiceImpl implements IMemberInfoService {
    @Autowired
    private MemberInfoMapper memberInfoMapper;
    @Autowired
    private AccountBookMapper accountBookMapper;
    @Override
    public void saveOrUpdate(MemberInfo entity) {
        if (entity.getId() != null) {
            //更新操作
            memberInfoMapper.updateByPrimaryKey(entity);


        }else {
            //新增会员 默认会员等级是最低的,系统默认就行
            entity.setDate(new Date());
            memberInfoMapper.insert(entity);
            //给会员设置卡号id +1000
            long num = entity.getId() + 1000;
            memberInfoMapper.updateCardNum(num,entity.getId());

        }

    }

    @Override
    public void changeState(Long id) {
        memberInfoMapper.changeState(id);
    }

    @Override
    public MemberInfo get(Long id) {
        MemberInfo memberInfo = memberInfoMapper.selectByPrimaryKey(id);
        return memberInfo;
    }

    @Override
    public List<MemberInfo> selectAll() {
        List<MemberInfo> list = memberInfoMapper.selectAll();
        return list;
    }


    @Override
    public MemberInfo queryByCardNumber(String cardNumber) {
        return memberInfoMapper.selectByCardNumber(cardNumber);

    }

    /**
     * 会员消费,改变会员卡上余额
     * @param id 指定的会员id
     */
    @Override
    public void vipConsume(Long id ,BigDecimal consumption,Long billId) {
        //通过id查询指定会员信息
        MemberInfo vip = memberInfoMapper.selectByPrimaryKey(id);
        //判断消费多少钱等于多少积分
        BigDecimal points = vip.getPoints();
        vip.setPoints(consumption.setScale(0, RoundingMode.HALF_UP).add(points));
        //会员卡是否有效
        if (!vip.isState()) {

            throw new RuntimeException("会员卡已失效,请更换付款方式");
        }
        //余额
        if (vip.getBalance().compareTo(consumption)<0) {
            //余额不足,抛出异常
            throw new RuntimeException("余额不足,请充值");
        }
        //钱够
        BigDecimal balance = vip.getBalance().subtract(consumption);
        vip.setBalance(balance);
        //总消费累计
        BigDecimal totalConsume = vip.getConsumption().add(consumption);
        vip.setConsumption(totalConsume);
        //会员等级判断
        if (totalConsume.compareTo(new BigDecimal("2500"))>0) {
            //升级为黄金会员
            vip.setRankId("黄金vip");
            vip.setCutoff(new BigDecimal("0.97"));
        }
        if (totalConsume.compareTo(new BigDecimal("5000"))>0) {
            //升级为白金会员
            vip.setRankId("白金vip");
            vip.setCutoff(new BigDecimal("0.97"));
        }
        if (totalConsume.compareTo(new BigDecimal("5000"))>0) {
            //升级为钻石会员
            vip.setRankId("钻石vip");
            vip.setCutoff(new BigDecimal("0.95"));
        }
        //会员消费入总账
        AccountBook accountBook = new AccountBook();
        accountBook.setSn(true);
        accountBook.setAmount(consumption);
        accountBook.setBillId(billId);
        accountBook.setInputTime(new Date());
        memberInfoMapper.updateVipConsume(vip);
        accountBookMapper.insert(accountBook);
    }

    @Override
    public Map<String, Object> selectData() {
      Map<String, Object>  map= memberInfoMapper.selectData();
        BigDecimal consumption = (BigDecimal) map.get("consumption");
        MemberInfo vip =memberInfoMapper.selectByConsumption(consumption);
        String name = vip.getName();
        map.put("name",name);

        return map;
    }

    @Override
    public PageResult selectMemberConsumeInfo(MemberInfoQueryObject qo) {
        int rows = memberInfoMapper.rows(qo);
        if (rows==0) {
            return PageResult.EMPTY_RESULT;
        }
        PageResult result = new PageResult(rows, memberInfoMapper.selectMemberConsumeInfo(qo));
        return result;
    }

    @Override
    public PageResult queryForList(QueryObject qo) {
        int rows = memberInfoMapper.rows(qo);
        if (rows==0) {
            return PageResult.EMPTY_RESULT;
        }
        PageResult result = new PageResult(rows, memberInfoMapper.queryForList(qo));
        return result;
    }
}
